package org.example.hansp.genetic.exercise;

import org.junit.jupiter.api.Test;

import java.util.*;

public class GenericExercise02 {
    public static void main(String[] args) {
        Dao<User> dao = new Dao<User>();
        dao.save("1001", new User(1, 19, "lim"));
        dao.save("1002", new User(1, 29, "elias"));

        System.out.println(dao.get("1001"));
        dao.update("1002", new User(1, 29, "frank"));
        List<User> list = dao.list();
        for (User user : list) {
            System.out.println(user);
        }
        dao.delete("1002");

    }
}

class Dao<T> {
    Map<String, T> map = new HashMap<String, T>();

    public void save(String id, T entity) {
        map.put(id, entity);
    }

    public T get(String id) {
        return map.get(id);
    }

    public void update(String id, T entity) {
        map.put(id, entity);
    }

    public List<T> list() {
        return new ArrayList<T>(map.values());
    }
/*public List<T> list(){
      ArrayList<T> arrayList=new ArrayList<>();
    Set<String> strings = map.keySet();
    for (String key : strings) {
        arrayList.add(map.get(key));
    }
    return arrayList;
}*/
    public void delete(String id) {
        map.remove(id);
    }
}

class User {
    private int id;
    private int age;
    private String name;

    public User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + '}';
    }
}